/**
 * 
 */
package com.manthano.util.search;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;

/**
 * @author DELL
 * @param <E>
 */
public class SearchResult<E> implements Serializable
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 5635923834674243025L;
	private int					totalCount			= -1;
	private Object				result				= null;

	/**
	 * 
	 */
	public SearchResult()
	{}

	/**
	 * @return the totalCount
	 */
	public int getTotalCount()
	{
		return totalCount;
	}

	/**
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount)
	{
		this.totalCount = totalCount;
	}

	/**
	 * @return the result
	 */
	@SuppressWarnings("unchecked")
	public E getResult()
	{
		return (E) result;
	}

	/**
	 * @param result
	 *            the result to set
	 */
	public void setResult(E result)
	{
		this.result = result;
	}

	/**
	 * @param jdbcTemplate
	 * @param query
	 * @param parameters
	 * @param resultSetExtractor
	 */
	public void prepareSearchResult(JdbcTemplate jdbcTemplate, final String query, final List<Object> parameters,
			final ResultSetExtractor<E> resultSetExtractor)
	{
		result = jdbcTemplate.query(new PreparedStatementCreator()
		{
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException
			{
				return connection.prepareStatement(query);
			}
		}, new PreparedStatementSetter()
		{
			public void setValues(PreparedStatement preparedStatement) throws SQLException
			{
				int index = 0;
				for (Object parameter : parameters)
				{
					if (parameter instanceof String)
					{
						preparedStatement.setString(++index, (String) parameter);
					}
					else if (parameter instanceof Timestamp)
					{
						preparedStatement.setTimestamp(++index, (Timestamp) parameter);
					}
					else if (parameter instanceof Integer)
					{
						preparedStatement.setInt(++index, (Integer) parameter);
					}
					else if (parameter instanceof Double)
					{
						preparedStatement.setDouble(++index, (Double) parameter);
					}
					else if (parameter instanceof BigDecimal)
					{
						preparedStatement.setBigDecimal(++index, (BigDecimal) parameter);
					}
					else if (parameter instanceof Long)
					{
						preparedStatement.setLong(++index, (Long) parameter);
					}
				}
			}
		}, resultSetExtractor);
	}

	/**
	 * @param jdbcTemplate
	 * @param countSql
	 * @param parameters
	 */
	@SuppressWarnings("deprecation")
	public void prepareSearchCount(JdbcTemplate jdbcTemplate, String countSql, Object[] parameters)
	{
		totalCount = jdbcTemplate.queryForInt(countSql, parameters);
	}
}
